package com.b511.swpu.cementbackend.util;

import com.b511.swpu.cementbackend.util.enumeration.Code;

import java.util.HashMap;
import java.util.Map;

public class R extends HashMap<String, Object> {
    public R() {
        put("code", Code.SUCCESS.getCode());
        put("msg", Code.SUCCESS.getMsg());
    }

    public static R error() {
        return error(Code.FAIL.getCode(), Code.FAIL.getMsg());
    }

    public static R error(Code code) {
        return error(code.getCode(), code.getMsg());
    }

    public static R error(String msg) {
        return error(Code.FAIL.getCode(), msg);
    }

    public static R error(int code, String msg) {
        R r = new R();
        r.put("code", code);
        r.put("msg", msg);
        return r;
    }

    public static R ok(String msg) {
        R r = new R();
        r.put("msg", msg);
        return r;
    }

    public static R ok(Map<String, Object> map) {
        R r = new R();
        r.putAll(map);
        return r;
    }

    public static R ok() {
        return new R();
    }

    @Override
    public R put(String key, Object value) {
        super.put(key, value);
        return this;
    }

    public R putData(Object value) {
        super.put("data", value);
        return this;
    }

}
